﻿// 功能：扩展列表项集合
// 描述：
// 作者：温伟鹏
// 日期：2010/9/20 10:47:40

using System;
using System.Collections.Generic;
using System.Text;

namespace MeizuSDK.Presentation
{
    /// <summary>
    /// 扩展列表项集合
    /// </summary>
    public class ListItemExCollection : ListItemCollection
    {
        /// <summary>
        /// 获取列表项选择模式
        /// </summary>
        public ListBoxSelectMode SelectMode
        {
            get;
            protected set;
        }

        #region 构造函数
        /// <summary>
        /// 构建扩展列表项集合对象
        /// </summary>
        /// <param name="owner"></param>
        public ListItemExCollection(ListBoxEx owner)
            : base(owner)
        {
            SelectMode = owner.SelectMode;
        }
        #endregion

        #region 函数
        /// <summary>
        /// 设置集合中被选中的列表项
        /// </summary>
        /// <param name="index">列表项索引</param>
        /// <param name="selected">是否选中</param>
        public override void SetItemSelected(int index, bool selected)
        {
            if (index >= Count)
                return;

            switch (SelectMode)
            {
                case ListBoxSelectMode.None:
                case ListBoxSelectMode.Single:
                    {
                        if (index < 0)
                        {
                            lock (this)
                            {
                                foreach (ListItem item in List)
                                {
                                    item.Selected = false;
                                }
                            }
                        }

                        lock (this)
                        {
                            for (int i = 0; i < Count; i++)
                            {
                                if (i != index)
                                    List[i].Selected = !selected;
                                else
                                    List[i].Selected = selected;
                            }
                        }
                    }
                    break;
                case ListBoxSelectMode.Multiple:
                    {
                        if (index < 0)
                            return;

                        List[index].Selected = selected;
                    }
                    break;
            }
        }
        #endregion
    }
}
